<template>
  <div ref="content" class="content">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px" :class="{header:needTagsView && fixedHeader}">
      <el-form-item label="角色名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入角色名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="tableData.filter(data => !search || data.houseResidentialQuarters.toLowerCase().includes(search.toLowerCase()))"
      tooltip-effect="dark"
      height="600px"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        prop="roleId"
        label="角色ID"
        width="120"
      />
      <el-table-column
        prop="roleName"
        label="角色名称"
      />
      <el-table-column
        prop="roleDescription"
        label="角色描述"
      />
      <el-table-column
        prop="createTime"
        label="创建时间"
      />
      <el-table-column
        fixed="right"
      >
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="查看此角色的数据" placement="bottom-end">
            <el-button
              type="text"
              size="small"
              @click.native.prevent="lookUp(scope.$index, tableData)"
            >
              查看
            </el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="修改此角色的数据" placement="bottom-end">
            <el-button
              type="text"
              size="small"
              @click.native.prevent="update(scope.$index, tableData)"
            >
              修改
            </el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除此角色的数据" placement="bottom-end">
            <el-button
              type="text"
              size="small"
              @click.native.prevent="deleteRow(scope.$index, tableData)"
            >
              删除
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="text-align:center"
      background
      :page-sizes="[20, 40, 80, 100, 200]"
      :page-size="20"
      layout="sizes, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- dialog 查看角色信息 -->
    <el-dialog title="角色信息" :visible.sync="dialogVisible" :before-close="formClose">
      <el-form :model="form">
        <el-form-item label="角色ID" :label-width="formLabelWidth">
          <el-input v-model="form.roleId" autocomplete="off" readonly />
        </el-form-item>
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="form.roleName" autocomplete="off" :readonly="!formState" />
        </el-form-item>
        <el-form-item label="角色描述" :label-width="formLabelWidth">
          <el-input v-model="form.roleDescription" autocomplete="off" :readonly="!formState" />
        </el-form-item>
        <el-form-item label="创建时间" :label-width="formLabelWidth">
          <el-input v-model="form.createTime" autocomplete="off" readonly />
        </el-form-item>
      </el-form>
      <div v-if="formState" slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getAllRole, deleteRole, updateRole } from '@/api/role'
export default {
  components: {},
  data() {
    return {
      loading: false,
      total: 0,
      pageSize: 20,
      pageCurrent: 1,
      tableData: [],
      multipleSelection: [],
      dialogVisible: false,
      formLabelWidth: '120px',
      form: {},
      formState: false,
      search: '',
      // 是否在操作列上方显示搜索
      showSearch: true,
      queryParams: {
        userName: undefined
      }
    }
  },
  computed: {
    ...mapState({
      needTagsView: state => state.settings.tagsView,
      fixedHeader: state => state.settings.fixedHeader
    })
  },
  created() {
    this.init()
  },
  methods: {
    // 初始化函数
    init() {
      this.loading = true
      getAllRole(this.pageSize, this.pageCurrent).then(resp => {
        this.total = resp.obj.total
        this.tableData = resp.obj.list
        this.loading = false
      })
    },
    // 左侧选择框
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 删除行
    deleteRow(index, rows) {
      this.deleteConfirm(index, rows)
    },
    // 查看信息
    lookUp(index, rows) {
      this.form = JSON.parse(JSON.stringify(rows[index]))
      this.dialogVisible = true
    },
    // 修改信息
    update(index, rows) {
      this.form = JSON.parse(JSON.stringify(rows[index]))
      this.formState = true
      this.dialogVisible = true
    },
    // 弹窗表单关闭回调函数
    formClose() {
      this.formState = false
      this.dialogVisible = false
    },
    //  更新二次确认弹窗
    updateConfirm() {
      this.$confirm('确定要更新数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateRole(this.form).then(rsp => {
          if (rsp.result) {
            this.$message({
              type: 'success',
              message: '更新成功'
            })
          } else {
            this.$notify({
              title: '操作失败',
              message: '更新角色数据失败，请联系管理员',
              type: 'warning'
            })
          }
          this.dialogVisible = false
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
        this.formState = false
      })
    },
    // 删除二次确认弹窗
    deleteConfirm(index, rows) {
      this.$confirm('确定要删除此角色数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRole(rows[index].roleId).then(rsp => {
          if (rsp.result) {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.dialogVisible = false
            rows.splice(index, 1)
          } else {
            this.$notify({
              title: '操作失败',
              message: '删除角色失败，请联系管理员',
              type: 'warning'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },

    // 改变分页数量
    handleSizeChange(val) {
      this.loading = true
      this.pageCurrent = 1
      getAllRole(val, 1).then(resp => {
        this.total = resp.obj.total
        this.tableData = resp.obj.list
        this.loading = false
      })
    },
    // 换页
    handleCurrentChange(val) {
      this.loading = true
      getAllRole(this.pageSize, val).then(resp => {
        this.total = resp.obj.total
        this.tableData = resp.obj.list
        this.loading = false
      })
    },
    // 增加角色
    handleAdd() {

    },
    // 更新角色信息
    handleUpdate() {

    },
    // 删除角色信息
    handleDelete() {

    },
    // 搜索按钮操作
    handleQuery() {

    },
    // 重置按钮操作
    resetQuery() {
      this.queryParams.userName = ''
    }
  }
}
</script>

<style>
 .content {
   margin: 20px;
 }
 .header {
   margin-top: 54px;
 }
</style>
